<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
//exit();
$strWeb = $_SERVER[argv][1];
$_SERVER["DOCUMENT_ROOT"] = "/home/share/www/".$strWeb."/secure";
$strError = $strWeb."  (".date("Y-m-d H:i:s").")\n";
$strError .= "===========================================\n";

$_isDBExist = "no";
$_DBSite = "Master";
if(!file_exists($_SERVER["DOCUMENT_ROOT"]."/config.inc.php"))exit($strError." no config.inc.php");
include("/home/share/www/SourceSite-utf8/secure/config.inc.php");
include(__Root_Path."/config.inc.php");
include(__DBConnect_File);

if($_strLogErr != ""){
	@mysql_close($$_Conn);
	exit($strError.$strLogError.$_strLogErr);
}
if(!preg_match("/utf8/i", $_blDBName))exit($strError." no UTF8 DB");
$strDBSite = $_blDBName;
$strDBSite = preg_replace("/^bet/", "", $strDBSite);
$strDBSite = preg_replace("/UTF8$/", "", $strDBSite);

//連結使用者資料中心
global $_ConnUser;
$strHost = "192.168.5.127";
$strDBUser = "kacino";
$strDBPass = "cyriac_0426";
$strDBName = "betLottoUser";

$arrErr = OtherDBConnection($_ConnUser, $strHost, $strDBUser, $strDBPass, $strDBName);
if(sizeof($arrErr) > 0){
	$strError .= "Err1: ".$arrErr["Subject"]."\n";
	$strError .= "ErrCon1: ".$arrErr["Content"]."\n";
	exit($strError);
}


//從資料中心找出最新更新時間
$strSQL = "select `UpdateTime` from `outUserData` where `DBSite` = '".$strDBSite."' order by `UpdateTime` desc limit 1";
if(!$RS = mysql_query($strSQL, $_ConnUser)){
	$strError .= "SQL: ".$strSQL."<br>\n";
	$strError .= "Error: ".mysql_error($_ConnUser)."<br>\n";
	exit($strError);
}
$intUpdateTime = 0;
while($Rows = mysql_fetch_object($RS)){
	$intUpdateTime = $Rows -> UpdateTime;
}


$intT = time();
while(true){
	//找出現有資料更新在此時間之後
	$strSQL = "select `ID` as UserID, `Account` as UserAcc, `Password` as UserPwd, `MemberGroup` as UserGroup, `isLock` as UserStatus, `SpGroup`, `UpdateTime`  from `bet_Member` where `UpdateTime` > ".$intUpdateTime." or `UpdateTime` = 0 order by `UpdateTime`";
	if(!$RS = mysql_query($strSQL, $_ConnMaster)){
		$strError .= $strWeb."  (".date("Y-m-d H:i:s").") => SQL: ".$strSQL."  Error: ".mysql_error($_ConnMaster)."<br>\n";
		exit($strError);
	}

	$arrUser = array();
	$arrNewUSerID = array();
	while($objUser = mysql_fetch_object($RS)){
		if($objUser -> UpdateTime == 0){
			$arrNewUSerID[] = $objUser -> UserID;
		}
		$arrUser[] = "('".$objUser -> UserAcc."', '".$strDBSite."', '".$objUser -> UserPwd."', '".md5($objUser -> UserAcc)."', '".md5($objUser -> UserPwd)."', '".$objUser -> UserGroup."', '".$objUser -> UserStatus."', '".$objUser -> SpGroup."', '".$objUser -> UpdateTime."', 0, UNIX_TIMESTAMP())";

		if($intUpdateTime < $objUser -> UpdateTime)$intUpdateTime = $objUser -> UpdateTime;

		$intNo = sizeof($arrUser);
		if($intNo >= 2000){
			$strError .= "No => ".$intNo."\n";

			$strSQL = "insert into `outUserData`(`UserAcc`, `DBSite`, `UserPwd`, `EncodeAcc`, `EncodePwd`, `UserGroup`, `UserStatus`, `SpGroup`, `UpdateTime`, `ComID`, `SelfUpdateTime`) values";
			$strSQL .= implode(", ", $arrUser);
			$strSQL .= "ON DUPLICATE KEY UPDATE UserPwd = VALUES(UserPwd), EncodePwd = VALUES(EncodePwd), UserGroup = VALUES(UserGroup), UserStatus = VALUES(UserStatus), SpGroup = VALUES(SpGroup), UpdateTime = VALUES(UpdateTime), SelfUpdateTime = UNIX_TIMESTAMP()";

			if(!mysql_query($strSQL, $_ConnUser)){
				$strError .= $strWeb."  (".date("Y-m-d H:i:s").") => SQL: ".$strSQL."  Error: ".mysql_error($_ConnUser)."<br>\n";
			}
			$arrUser = array();
		}
	}

	$intNo = sizeof($arrUser);
	$strError .= "No => ".$intNo."\n";

	if($intNo > 0){
		$strSQL = "insert into `outUserData`(`UserAcc`, `DBSite`, `UserPwd`, `EncodeAcc`, `EncodePwd`, `UserGroup`, `UserStatus`, `SpGroup`, `UpdateTime`, `ComID`, `SelfUpdateTime`) values";
		$strSQL .= implode(", ", $arrUser);
		$strSQL .= "ON DUPLICATE KEY UPDATE UserPwd = VALUES(UserPwd), EncodePwd = VALUES(EncodePwd), UserGroup = VALUES(UserGroup), UserStatus = VALUES(UserStatus), SpGroup = VALUES(SpGroup), UpdateTime = VALUES(UpdateTime), SelfUpdateTime = UNIX_TIMESTAMP()";

		if(!mysql_query($strSQL, $_ConnUser)){
			$strError .= $strWeb."  (".date("Y-m-d H:i:s").") => SQL: ".$strSQL."  Error: ".mysql_error($_ConnUser)."<br>\n";
		}
	}

	//若有存在更新時間等於 0, 就更新其時間
	if(sizeof($arrNewUSerID) > 0){
		$strSQL = "update `bet_Member` set `UpdateTime` = ".($intUpdateTime - 60)." where `ID` in (".implode(",", $arrNewUSerID).")";
		if(!mysql_query($strSQL, $_ConnMaster)){
			$strError .= $strWeb."  (".date("Y-m-d H:i:s").") => SQL: ".$strSQL."  Error: ".mysql_error($_ConnMaster)."<br>\n";
		}
	}

	if(time() - $intT >= 55)break;
	$strError .= "sleep => 5\n";
	sleep(5);
}
print $strError."\nEnd (".date("Y-m-d H:i:s").")\n";
?>